from decimal import Decimal as Dec

class DataTool:

    @staticmethod
    def recursionsearch(targetdict, serchkey):
        # 在targetdict 中寻找key为serchkey 的value并返回
        for k,v in targetdict.items():
            if k == serchkey:
                return v
            elif isinstance(v, dict):
                ret = DataTool().recursionsearch(v, serchkey)
                if ret is not None:
                    return ret
    
    @staticmethod
    def feefillin(datadict, name, value):
        # 把 value写入key为name的datadict
        for i in datadict:
            if not isinstance(datadict[i], dict):
                if i == name:
                    datadict[i] = value
            else:
                DataTool().feefillin(datadict[i], name, value)

    @staticmethod
    def sum_dict(d):
        num = Dec('0')
        if isinstance(d, dict):
            for i in d:
                if isinstance(d[i], dict):
                    num += Dec(str(DataTool().sum_dict(d[i])))
                else:
                    if not isinstance(d[i], list):
                        num += Dec(str(d[i]))
        else:
            num = Dec(str(d))
        return num

    @staticmethod
    def table2parseformula(formula):
        # 将公式字符串转换为列表（数据和符号分开）
        sign = '()+-*/'
        temp = ''
        forlist = []
        if formula == '':
            return ''
        for index, lastword in enumerate(formula):
            if lastword not in sign:
                temp += lastword                
            else:
                if temp != '':
                    forlist.append(temp)
                    temp = ''
                if index != len(formula) - 1:
                    forlist.append(lastword)
            if index == len(formula) - 1:
                if temp != '':
                    forlist.append(temp)
                    temp = ''
                else:
                    forlist.append(lastword)
        return forlist

    @staticmethod
    def listadddec(thislist):
        # 在formulalist 中的数字套上decimal
        sign = '()+-*/'
        for index, i in enumerate(thislist):
            i = str(i)
            if i not in sign:
                thislist[index] = 'Dec("' + str(float(i)) + '")'
        return thislist

    @staticmethod
    def formula2list(formula):
        # formula转换为list好给listadddec用
        sign = '()+-*/%'
        ret = []
        value = ''
        for index, i in enumerate(formula):
            if index != len(formula) - 1:
                if i not in sign:
                    if value == '/':
                        value = i
                        ret.append('/')
                    else:
                        value += i
                else:
                    if value != '' and value != '/':
                        ret.append(value)
                        value = ''

                    if i != '/':
                        ret.append(i)
                    elif value == '/' and i == '/':
                        value = ''
                        ret.append('//')
                    else:
                        value += i
            else:
                if i == ')':
                    ret.append(value)
                    ret.append(')')
                else:
                    value += i
                    ret.append(value)
        return ret

    @staticmethod
    def list2str(thislist):
        
        if not isinstance(thislist, list):
            return ''
        restr = ''
        for i in thislist:
            if not isinstance(i,str):
                i = str(i)
            restr += i
        return restr

    @staticmethod
    def isnum(string):
        try:
            if string=='NaN':
                return False
            float(string)
            return True
        except ValueError:
            return False
    
    @staticmethod
    def allfillin(datadict, value):
        # 把 value写入key为name的datadict
        for i in datadict:
            if not isinstance(datadict[i], dict):
                datadict[i] = value
            else:
                DataTool().feefillin(datadict[i], value)

if __name__ == "__main__":
    formula = '0+10//100*(12+23*96/85%2)'
    print(formula)
    print(DataTool().formula2list(formula))